package com.lotus.lqmp.service.biz.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Maps;
import com.lotus.idaas.sdk.common.UserInfoContext;
import com.lotus.lqmp.base.biz.bean.PageQuery;
import com.lotus.lqmp.service.biz.BizModifyRecordBizService;
import com.lotus.lqmp.service.biz.DeliverablesTemplateConfigBizService;
import com.lotus.lqmp.service.common.convert.DeliverablesTemplateConfigConvert;
import com.lotus.lqmp.service.common.enums.ErrorCodeEnum;
import com.lotus.lqmp.service.common.enums.RecordBizOperateEnum;
import com.lotus.lqmp.service.common.enums.RecordBizTypeEnum;
import com.lotus.lqmp.service.common.utils.UuidUtil;
import com.lotus.lqmp.service.controller.vo.req.DeliverablesAddReq;
import com.lotus.lqmp.service.controller.vo.req.DeliverablesDeleteReq;
import com.lotus.lqmp.service.controller.vo.req.DeliverablesQueryReq;
import com.lotus.lqmp.service.controller.vo.req.DeliverablesUpdateReq;
import com.lotus.lqmp.service.controller.vo.resp.DeliverablesQueryResp;
import com.lotus.lqmp.service.dao.entity.DeliverablesTemplateConfigDo;
import com.lotus.lqmp.service.service.DeliverablesTemplateConfigService;
import com.lotus.platform.common.model.exception.BusinessException;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * description
 * @author Shuang.Pan2
 * @since 2022/03/31 13:13
 */
@Service
public class DeliverablesTemplateConfigBizServiceImpl implements DeliverablesTemplateConfigBizService {

    @Autowired
    private DeliverablesTemplateConfigService deliverablesTemplateConfigService;

    @Autowired
    private BizModifyRecordBizService bizModifyRecordBizService;

    @Autowired
    private DeliverablesTemplateConfigConvert deliverablesTemplateConfigConvert;

    private static final String NAME_KEY = "deliverablesName";
    private static final String OLD_NAME_KEY = "oldDeliverablesName";
    private static final String NEW_NAME_KEY = "newDeliverablesName";
    private static final String NEW_JR1_KEY = "newJr1Key";
    private static final String OLD_JR1_KEY = "oldJr1Key";
    private static final String NEW_JR2_KEY = "newJr2Key";
    private static final String OLD_JR2_KEY = "oldJr2Key";
    private static final String NEW_JR3_KEY = "newJr3Key";
    private static final String OLD_JR3_KEY = "oldJr3Key";
    private static final String NEW_JR4_KEY = "newJr4Key";
    private static final String OLD_JR4_KEY = "oldJr4Key";

    @Override
    public void add(DeliverablesAddReq deliverablesAddReq) {
        DeliverablesTemplateConfigDo deliverablesTemplateConfigDo = deliverablesTemplateConfigService.getExistByName(
            deliverablesAddReq.getDeliverablesName());
        if (deliverablesTemplateConfigDo != null) {
            throw new BusinessException(ErrorCodeEnum.DELIVERABLES_NAME_DUPLICATE);
        }

        saveDeliverablesTemplateConfig(deliverablesAddReq);

        Map<String, String> nameMap = Maps.newHashMap();
        nameMap.put(NAME_KEY, deliverablesAddReq.getDeliverablesName());

        bizModifyRecordBizService.saveModifyRecord(RecordBizOperateEnum.ADD, JSON.toJSONString(nameMap),
            deliverablesAddReq.getModifyReason(), RecordBizTypeEnum.DELIVERABLES_MODIFY);
    }

    @Override
    public void batchDeleteByIds(DeliverablesDeleteReq deliverablesDeleteReq) {
        if (CollectionUtils.isEmpty(deliverablesDeleteReq.getIds())) {
            return;
        }
        List<DeliverablesTemplateConfigDo> deliverablesTemplateConfigDos = deliverablesTemplateConfigService.listByIds(
            deliverablesDeleteReq.getIds());
        if (CollectionUtils.isEmpty(deliverablesTemplateConfigDos)) {
            return;
        }

        deliverablesTemplateConfigDos.forEach(curr -> {
            curr.setDeletedStatus(UuidUtil.getUUID());
            curr.setEditor(UserInfoContext.getUserInfo().getEmployeeNo());
        });

        deliverablesTemplateConfigService.updateBatchById(deliverablesTemplateConfigDos);

        List<String> modifyNames = deliverablesTemplateConfigDos.stream()
            .map(DeliverablesTemplateConfigDo::getDeliverablesName).collect(Collectors.toList());

        Map<String, List<String>> nameMap = Maps.newHashMap();
        nameMap.put(NAME_KEY, modifyNames);

        bizModifyRecordBizService.saveModifyRecord(RecordBizOperateEnum.DELETE, JSON.toJSONString(nameMap),
            deliverablesDeleteReq.getModifyReason(), RecordBizTypeEnum.DELIVERABLES_MODIFY);

    }

    @Override
    public void update(DeliverablesUpdateReq deliverablesUpdateReq) {
        DeliverablesTemplateConfigDo deliverablesTemplateConfigDo = deliverablesTemplateConfigService.getByExistById(
            deliverablesUpdateReq.getId());
        if (deliverablesTemplateConfigDo == null) {
            throw new BusinessException(ErrorCodeEnum.DELIVERABLES_NOT_EXIST);
        }

        DeliverablesTemplateConfigDo existNameInfo = deliverablesTemplateConfigService.getExistByName(
            deliverablesUpdateReq.getDeliverablesName());

        if (existNameInfo != null && !existNameInfo.getId().equals(deliverablesUpdateReq.getId())) {
            throw new BusinessException(ErrorCodeEnum.DELIVERABLES_NAME_DUPLICATE);
        }

        String modifyJson = buildModifyContent(deliverablesUpdateReq, deliverablesTemplateConfigDo);

        updateTemplateConfigInfo(deliverablesUpdateReq, deliverablesTemplateConfigDo);

        bizModifyRecordBizService.saveModifyRecord(RecordBizOperateEnum.UPDATE, modifyJson,
            deliverablesUpdateReq.getModifyReason(), RecordBizTypeEnum.DELIVERABLES_MODIFY);
    }

    @Override
    public DeliverablesQueryResp getById(long id) {
        DeliverablesTemplateConfigDo deliverablesTemplateConfigDo = deliverablesTemplateConfigService.getByExistById(
            id);
        return deliverablesTemplateConfigDo == null ? null
            : deliverablesTemplateConfigConvert.do2Resp(deliverablesTemplateConfigDo);
    }

    @Override
    public Page<DeliverablesQueryResp> queryForPage(PageQuery<DeliverablesQueryReq> queryInfo) {
        Page<DeliverablesTemplateConfigDo> doPage = deliverablesTemplateConfigService.queryForPage(queryInfo);
        Page<DeliverablesQueryResp> respPage = new Page<>(queryInfo.getPageNum(), queryInfo.getPageSize());
        respPage.setTotal(doPage.getTotal());
        respPage.setRecords(deliverablesTemplateConfigConvert.dos2Resps(doPage.getRecords()));
        return respPage;
    }

    private String buildModifyContent(DeliverablesUpdateReq deliverablesUpdateReq,
        DeliverablesTemplateConfigDo deliverablesTemplateConfigDo) {
        Map<String, String> modifyMap = Maps.newHashMap();
        if (deliverablesUpdateReq.getDeliverablesName().equals(deliverablesTemplateConfigDo.getDeliverablesName())) {
            modifyMap.put(NAME_KEY, deliverablesUpdateReq.getDeliverablesName());
        } else {
            modifyMap.put(OLD_NAME_KEY, deliverablesTemplateConfigDo.getDeliverablesName());
            modifyMap.put(NEW_NAME_KEY, deliverablesUpdateReq.getDeliverablesName());
        }

        if (!deliverablesUpdateReq.getJr1Config().equals(deliverablesTemplateConfigDo.getJr1Config())) {
            modifyMap.put(OLD_JR1_KEY, deliverablesTemplateConfigDo.getJr1Config().toString());
            modifyMap.put(NEW_JR1_KEY, deliverablesUpdateReq.getJr1Config().toString());
        }

        if (!deliverablesUpdateReq.getJr2Config().equals(deliverablesTemplateConfigDo.getJr2Config())) {
            modifyMap.put(OLD_JR2_KEY, deliverablesTemplateConfigDo.getJr2Config().toString());
            modifyMap.put(NEW_JR2_KEY, deliverablesUpdateReq.getJr2Config().toString());
        }

        if (!deliverablesUpdateReq.getJr3Config().equals(deliverablesTemplateConfigDo.getJr3Config())) {
            modifyMap.put(OLD_JR3_KEY, deliverablesTemplateConfigDo.getJr3Config().toString());
            modifyMap.put(NEW_JR3_KEY, deliverablesUpdateReq.getJr3Config().toString());
        }

        if (!deliverablesUpdateReq.getJr4Config().equals(deliverablesTemplateConfigDo.getJr4Config())) {
            modifyMap.put(OLD_JR4_KEY, deliverablesTemplateConfigDo.getJr4Config().toString());
            modifyMap.put(NEW_JR4_KEY, deliverablesUpdateReq.getJr4Config().toString());
        }

        return JSON.toJSONString(modifyMap);
    }

    private void updateTemplateConfigInfo(DeliverablesUpdateReq deliverablesUpdateReq,
        DeliverablesTemplateConfigDo deliverablesTemplateConfigDo) {
        deliverablesTemplateConfigDo.setJr1Config(deliverablesUpdateReq.getJr1Config());
        deliverablesTemplateConfigDo.setJr2Config(deliverablesUpdateReq.getJr2Config());
        deliverablesTemplateConfigDo.setJr3Config(deliverablesUpdateReq.getJr3Config());
        deliverablesTemplateConfigDo.setJr4Config(deliverablesUpdateReq.getJr4Config());
        deliverablesTemplateConfigDo.setDeliverablesName(deliverablesUpdateReq.getDeliverablesName());
        deliverablesTemplateConfigDo.setEditor(UserInfoContext.getUserInfo().getEmployeeNo());
        deliverablesTemplateConfigService.updateById(deliverablesTemplateConfigDo);
    }

    private void saveDeliverablesTemplateConfig(DeliverablesAddReq deliverablesAddReq) {
        DeliverablesTemplateConfigDo deliverablesTemplateConfigDo = new DeliverablesTemplateConfigDo();
        deliverablesTemplateConfigDo.setDeliverablesName(deliverablesAddReq.getDeliverablesName());
        deliverablesTemplateConfigDo.setJr1Config(deliverablesAddReq.getJr1Config());
        deliverablesTemplateConfigDo.setJr2Config(deliverablesAddReq.getJr2Config());
        deliverablesTemplateConfigDo.setJr3Config(deliverablesAddReq.getJr3Config());
        deliverablesTemplateConfigDo.setJr4Config(deliverablesAddReq.getJr4Config());
        deliverablesTemplateConfigDo.setCreator(UserInfoContext.getUserInfo().getEmployeeNo());
        deliverablesTemplateConfigDo.setEditor(UserInfoContext.getUserInfo().getEmployeeNo());
        deliverablesTemplateConfigService.save(deliverablesTemplateConfigDo);
    }
}
